//
//  WorkmateMainVC.m
//  MyProject
//
//  Created by 石小强 on 2018/3/23.
//  Copyright © 2018年 yanjiaming. All rights reserved.
//

#import "WorkmateMainVC.h"
#import "AnswersTableViewCell.h"
#import "WorkmateEntryInsurance.h"
#import "WorkmateDecryptionView.h"
#import "EmployeeCardViewController.h"
#import "StringAndIndexModel.h"
#import "ReportOrCancelViewController.h"
#import "WorkmateModel.h"
#import "IncumbentQuestionModel.h"
#import "DecryptModel.h"
#import "AssurePassModel.h"
#import "QuestionModel.h"
#import "FilterButton.h"
#import "EntryProtectDetailVC.h"
#import "DirectionalQuestionViewController.h"
#import "NSUDManager.h"
#import "QuestionAnwserModel.h"
#import "PlayVoice.h"
#import "HelpDetailVC.h"

@interface WorkmateMainVC ()<UIScrollViewDelegate>

@property (nonatomic, strong) WorkmateModel *workmateModel;
@property (nonatomic, strong) NSMutableArray *questionArray;
@property (nonatomic, strong) NSMutableArray *decryptArray;
@property (nonatomic, strong) NSMutableArray *assurePassArray;
@property (nonatomic, strong) NSMutableArray *reviewArray;

@end

@implementation WorkmateMainVC {
    NSInteger _pageNo;
}

- (void)viewDidLoad {
    [super viewDidLoad];
//    self.navigationController.navigationBarHidden = YES;
 
    NSString *version = [UIDevice currentDevice].systemVersion;
    if (version.doubleValue >= 11.0) {
        // 针对 9.0 以上的iOS系统进行处理
        self.navViewTopHeight.constant = 0;
    } else {
        // 针对 9.0 以下的iOS系统进行处理
        self.navViewTopHeight.constant = 20;
    }
    
    [self setupSubViews];
    
    [self initDecryptionViews];
    [self initEntryInsuranceViews];
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        _pageNo = 1;
        [self loadUserInfoData];
        [self loadIncumbentQuestions];
        [self loadDecryptData];
        [self loadAssurePassData];
        [self loadAnswersDataList];
    }];
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        _pageNo += 1;
        [self loadAnswersDataList];
    }];
    
    [self getUserCollected];
    [self.tableView.mj_header beginRefreshing];
    
//    self.entryInsurancePageControl.backgroundColor = [UIColor redColor];
}

- (void)setupSubViews {
    if ([Tool isIphoneX]) {
        self.tableHeaderViewTopConstant.constant = 69 + 24;
        self.tableHeaderView.frame = CGRectMake(0, 0, kScreenWidth, 739 + 24);
    } else {
        self.tableHeaderViewTopConstant.constant = 69;
        self.tableHeaderView.frame = CGRectMake(0, 0, kScreenWidth, 739);
    }
    
    self.headerIconIV.layer.cornerRadius = 31.5;
    self.headerIconIV.layer.masksToBounds = YES;
    
    self.tableView.tableHeaderView = self.tableHeaderView;
    
    self.tableView.estimatedRowHeight = 184.0f;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([AnswersTableViewCell class]) bundle:nil] forCellReuseIdentifier:@"WorkmateAnswersTableViewCell"];
}


- (IBAction)collectionBtnClick:(id)sender {
    if (self.collectBtn.selected) {
        // 取消收藏
        WS(weakSelf)
        [self showProgressWithView:self.view animated:YES];
        [SendRequest cancelCollectWithRelatedId:self.userId result:^(NSDictionary *result, NSError *error) {
            [weakSelf hideProgress:self.view animated:YES];
            result = [CommonUtils decryptDicWithResult:result];
            if([result[@"result"] integerValue] == 1) {
                /**取消收藏成功*/
                self.collectBtn.selected = !self.collectBtn.selected;
            } else {
                [CommonUtils showToast:[result objectForKey:@"msg"]];
            }
        }];
    } else {
        // 收藏
        WS(weakSelf)
        [self showProgressWithView:self.view animated:YES];
        [SendRequest collectWithRelatedId:self.userId andType:@"COLLECTION_TYPE_USER" result:^(NSDictionary *result, NSError *error) {
            [weakSelf hideProgress:self.view animated:YES];
            result = [CommonUtils decryptDicWithResult:result];
            if([result[@"result"] integerValue] == 1) {
                /**收藏成功*/
                self.collectBtn.selected = !self.collectBtn.selected;
            } else {
                [CommonUtils showToast:[result objectForKey:@"msg"]];
            }
        }];
    }
}

- (IBAction)shareBtnClick:(id)sender {
    [self showShareView];
}

- (IBAction)tipBtnClick:(id)sender {
    NSArray *msgs = @[@"信息内容不真实", @"泄露/索取个人隐私", @"封建迷信、反动思想、涉黄涉暴等政治敏感内容", @"发布广告/虚假信息/传销等信息", @"辱骂/诽谤/恶意骚扰等人身攻击", @"其他"];
    NSMutableArray *dataArray = [[NSMutableArray alloc] init];
    for (NSInteger i = 0; i < msgs.count; i++) {
        StringAndIndexModel *model = [[StringAndIndexModel alloc] init];
        if (i == 0) {
            model.index = 1;
        } else {
            model.index = 0;
        }
        model.msg = msgs[i];
        [dataArray addObject:model];
    }
    
    ReportOrCancelViewController *VC = [[ReportOrCancelViewController alloc] init];
    VC.index = 1;
    VC.dataArray = dataArray;
    VC.relatedId = self.userId;
    VC.relatedType = @"REPORT_TYPE_INCUMBENT";
    [self.navigationController pushViewController:VC animated:YES];
}

/** 跳转到雇员小名片页 */
- (IBAction)cardBtnClick:(id)sender {
    EmployeeCardViewController *card = [[EmployeeCardViewController alloc] init];
    card.model = self.workmateModel;
    [self.navigationController pushViewController:card animated:YES];
}


#pragma mark - 私聊解密模块

- (void)initDecryptionViews {
    self.decryptionScrollView.contentSize = CGSizeMake((kScreenWidth - 128) * 4, 140);
    for (NSInteger i = 0; i < 4; i++) {
        WorkmateDecryptionView *wdV = [[WorkmateDecryptionView alloc] initWithFrame:CGRectMake(i * (kScreenWidth - 128), 0, kScreenWidth - 128, 140)];
        [self.decryptionScrollView addSubview:wdV];
    }
    
    self.decryptionPageControl.numberOfPages = 4;
    self.decryptionPageControl.currentPage = 0;
}

- (IBAction)decryptionLeftBtnClick:(id)sender {
    CGFloat scrollX = self.decryptionScrollView.contentOffset.x;
    
    [self.decryptionScrollView setContentOffset:CGPointMake(scrollX == 0 ? 0 : scrollX - (kScreenWidth - 128), 0) animated:YES];
}

- (IBAction)decryptionRightBtnClick:(id)sender {
    CGFloat scrollX = self.decryptionScrollView.contentOffset.x;
    
    [self.decryptionScrollView setContentOffset:CGPointMake(scrollX == (kScreenWidth - 128) * (self.decryptArray.count - 1) ? 0 : scrollX + (kScreenWidth - 128), 0) animated:YES];
}

#pragma mark - 入职保过模块

- (void)initEntryInsuranceViews {
    self.entryInsuranceScrollView.contentSize = CGSizeMake((kScreenWidth - 128) * 4, 140);
    for (NSInteger i = 0; i < 4; i++) {
        WorkmateEntryInsurance *eiV = [[WorkmateEntryInsurance alloc] initWithFrame:CGRectMake(i * (kScreenWidth - 128), 0, kScreenWidth - 128, 140)];
        [self.entryInsuranceScrollView addSubview:eiV];
    }
    
    self.entryInsurancePageControl.numberOfPages = 4;
    self.entryInsurancePageControl.currentPage = 0;
}

- (IBAction)entryInsuranceLeftBtnClick:(id)sender {
    CGFloat scrollX = self.entryInsuranceScrollView.contentOffset.x;
    
//    [self.entryInsuranceScrollView setContentOffset:CGPointMake(scrollX == 0 ? 0 : scrollX - (kScreenWidth - 128), 0)];
    [self.entryInsuranceScrollView setContentOffset:CGPointMake(scrollX == 0 ? 0 : scrollX - (kScreenWidth - 128), 0) animated:YES];
}

- (IBAction)entryInsuranceRightBtnClick:(id)sender {
    CGFloat scrollX = self.entryInsuranceScrollView.contentOffset.x;
    
    [self.entryInsuranceScrollView setContentOffset:CGPointMake(scrollX == (kScreenWidth - 128) * (self.assurePassArray.count - 1) ? 0 : scrollX + (kScreenWidth - 128), 0) animated:YES];
}

#pragma mark - 加载数据

/** 获取用户基础信息 */
- (void)loadUserInfoData {
    WS(weakSelf)
    [SendRequest getIncumbentInfoByUserId:self.userId result:^(NSDictionary *result, NSError *error) {
        result = [CommonUtils decryptDicWithResult:result];
        if([result[@"result"] integerValue] == 1) {
            SaintiLog(@"获取用户基础信息 = %@", [result objectForKey:@"data"]);
            WorkmateModel *model = [WorkmateModel mj_objectWithKeyValues:[[result objectForKey:@"data"] objectForKey:@"userInfo"]];
            weakSelf.workmateModel = model;
            NSDictionary *companyD = [[[result objectForKey:@"data"] objectForKey:@"userInfo"] objectForKey:@"companyInfo"];
            if (companyD) {
                CompanyModel *company = [CompanyModel mj_objectWithKeyValues:companyD];
                weakSelf.workmateModel.companyInfo = company;
                
//                weakSelf.workmateModel.companyInfo.realNameAuthentication = YES;
//                weakSelf.workmateModel.companyInfo.incumbentAuthentication = YES;
//                weakSelf.workmateModel.companyInfo.laborContractAuthentication = YES;
//                weakSelf.workmateModel.companyInfo.companyEamilAuthentication = YES;
//                weakSelf.workmateModel.companyInfo.employeeCardAuthentication = YES;
            }
//            weakSelf.workmateModel.companyInfo.realNameAuthentication = YES;
            
            [weakSelf resetHeaderView];
        } else {
            [CommonUtils showToast:[result objectForKey:@"msg"]];
        }
    }];
}

- (void)resetHeaderView {
    if (self.workmateModel.nickName && self.workmateModel.nickName.length > 0) {
        self.navTitleL.text = [NSString stringWithFormat:@"%@的主页", self.workmateModel.nickName];
    } else {
        self.navTitleL.text = @"TA的主页";
    }
    
    [self.headerIconIV sd_setImageWithURL:ImageURLWithKey(self.workmateModel.headImg) placeholderImage:PlaceholderImagePeople];
    self.headerNameL.text = self.workmateModel.nickName;
    if (self.workmateModel.companyInfo) {
        NSDateFormatter *ft = [[NSDateFormatter alloc] init];
        [ft setDateFormat:@"YYYY"];
        NSDate *dateNow = [NSDate date];
        NSString *currentTimeStr = [ft stringFromDate:dateNow];
        NSInteger time = [currentTimeStr integerValue] - [self.workmateModel.companyInfo.entryTime integerValue];
        
        self.headerPositionL.text = [NSString stringWithFormat:@"%@ | 已在职%zd年 | %@", self.workmateModel.companyInfo.jobName, time, self.workmateModel.companyInfo.regionName];
        self.headerCompanyL.text = self.workmateModel.companyInfo.companyName;
        
        NSInteger authenticationNum = 0;
        if (self.workmateModel.companyInfo.realNameAuthentication) {
            authenticationNum += 1;
        }
        if (self.workmateModel.companyInfo.companyEamilAuthentication) {
            authenticationNum += 1;
        }
        if (self.workmateModel.companyInfo.incumbentAuthentication) {
            authenticationNum += 1;
        }
        if (self.workmateModel.companyInfo.laborContractAuthentication) {
            authenticationNum += 1;
        }
        if (self.workmateModel.companyInfo.employeeCardAuthentication) {
            authenticationNum += 1;
        }
        if (authenticationNum > 0) {
            
            for (UIView *con in self.tableHeaderInfoView.subviews) {
                if (con.tag >= 1000 && con.tag <= 1005) {
                    [con removeFromSuperview];
                }
            }
            
            self.tableHeaderInfoViewLine.hidden = NO;
            
            if (authenticationNum > 3) {
                self.headerViewHeight.constant = 154;
                CGFloat X = 15;
                CGFloat Y = 104;
                CGFloat H = 20;
                
                CGFloat count = 0;
                if (self.workmateModel.companyInfo.incumbentAuthentication) {
                    NSString *titleStr = @"在职证明已认证";
                    CGSize titleS = [Tool sizeWithText:titleStr font:[UIFont fontWithName:@"PingFangSC-Regular" size:12]];
                    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(X, Y, titleS.width + 15 + 10, H)];
                    [btn setImage:[UIImage imageNamed:@"ic_work"] forState:UIControlStateNormal];
                    [btn setImage:[UIImage imageNamed:@"ic_work"] forState:UIControlStateHighlighted];
                    [btn setTitle:titleStr forState:UIControlStateNormal];
                    [btn setTitleColor:UIColorFromRGB(0x48565A) forState:UIControlStateNormal];
                    [btn setTitleColor:UIColorFromRGB(0x48565A) forState:UIControlStateHighlighted];
                    btn.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:12];
                    btn.tag = 1000;
                    [self.tableHeaderInfoView addSubview:btn];
                    [btn setTitleEdgeInsets:UIEdgeInsetsMake(0, 5, 0, 0)];
                
                    X += btn.mj_w + 25 * FitWidth;
                    count += 1;
                }
                if (self.workmateModel.companyInfo.realNameAuthentication) {
                    NSString *titleStr = @"实名认证";
                    CGSize titleS = [Tool sizeWithText:titleStr font:[UIFont fontWithName:@"PingFangSC-Regular" size:12]];
                    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(X, Y, titleS.width + 15 + 10, H)];
                    [btn setImage:[UIImage imageNamed:@"ic_实名认证"] forState:UIControlStateNormal];
                    [btn setImage:[UIImage imageNamed:@"ic_实名认证"] forState:UIControlStateHighlighted];
                    [btn setTitle:titleStr forState:UIControlStateNormal];
                    [btn setTitleColor:UIColorFromRGB(0x48565A) forState:UIControlStateNormal];
                    [btn setTitleColor:UIColorFromRGB(0x48565A) forState:UIControlStateHighlighted];
                    btn.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:12];
                    btn.tag = 1001;
                    [self.tableHeaderInfoView addSubview:btn];
                    [btn setTitleEdgeInsets:UIEdgeInsetsMake(0, 5, 0, 0)];
                    
                    X += btn.mj_w + 25 * FitWidth;
                    count += 1;
                }
                if (self.workmateModel.companyInfo.laborContractAuthentication) {
                    NSString *titleStr = @"劳动合同认证";
                    CGSize titleS = [Tool sizeWithText:titleStr font:[UIFont fontWithName:@"PingFangSC-Regular" size:12]];
                    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(X, Y, titleS.width + 15 + 10, H)];
                    [btn setImage:[UIImage imageNamed:@"合同"] forState:UIControlStateNormal];
                    [btn setImage:[UIImage imageNamed:@"合同"] forState:UIControlStateHighlighted];
                    [btn setTitle:titleStr forState:UIControlStateNormal];
                    [btn setTitleColor:UIColorFromRGB(0x48565A) forState:UIControlStateNormal];
                    [btn setTitleColor:UIColorFromRGB(0x48565A) forState:UIControlStateHighlighted];
                    btn.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:12];
                    btn.tag = 1002;
                    [self.tableHeaderInfoView addSubview:btn];
                    [btn setTitleEdgeInsets:UIEdgeInsetsMake(0, 5, 0, 0)];
                    
                    X += btn.mj_w + 25 * FitWidth;
                    count += 1;
                }
                if (self.workmateModel.companyInfo.companyEamilAuthentication) {
                    if (count >= 3) {
                        count = 1;
                        Y += 20;
                        X = 15;
                    }
                    NSString *titleStr = @"公司邮箱已认证";
                    CGSize titleS = [Tool sizeWithText:titleStr font:[UIFont fontWithName:@"PingFangSC-Regular" size:12]];
                    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(X, Y, titleS.width + 15 + 10, H)];
                    [btn setImage:[UIImage imageNamed:@"ic_mail"] forState:UIControlStateNormal];
                    [btn setImage:[UIImage imageNamed:@"ic_mail"] forState:UIControlStateHighlighted];
                    [btn setTitle:titleStr forState:UIControlStateNormal];
                    [btn setTitleColor:UIColorFromRGB(0x48565A) forState:UIControlStateNormal];
                    [btn setTitleColor:UIColorFromRGB(0x48565A) forState:UIControlStateHighlighted];
                    btn.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:12];
                    btn.tag = 1003;
                    [self.tableHeaderInfoView addSubview:btn];
                    [btn setTitleEdgeInsets:UIEdgeInsetsMake(0, 5, 0, 0)];
                    
                    X += btn.mj_w + 25;
                    count += 1;
                }
                if (self.workmateModel.companyInfo.companyEamilAuthentication) {
                    if (count >= 3) {
                        count = 1;
                        Y += 20;
                        X = 15;
                    }
                    NSString *titleStr = @"员工牌已认证";
                    CGSize titleS = [Tool sizeWithText:titleStr font:[UIFont fontWithName:@"PingFangSC-Regular" size:12]];
                    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(X, Y, titleS.width + 15 + 10, H)];
                    [btn setImage:[UIImage imageNamed:@"ic_yuangong"] forState:UIControlStateNormal];
                    [btn setImage:[UIImage imageNamed:@"ic_yuangong"] forState:UIControlStateHighlighted];
                    [btn setTitle:titleStr forState:UIControlStateNormal];
                    [btn setTitleColor:UIColorFromRGB(0x48565A) forState:UIControlStateNormal];
                    [btn setTitleColor:UIColorFromRGB(0x48565A) forState:UIControlStateHighlighted];
                    btn.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:12];
                    btn.tag = 1004;
                    [self.tableHeaderInfoView addSubview:btn];
                    [btn setTitleEdgeInsets:UIEdgeInsetsMake(0, 5, 0, 0)];
                    
                    X += btn.mj_w + 25;
                }
            } else {
                self.headerViewHeight.constant = 129;
                
                CGFloat X = 15;
                CGFloat Y = 94;
                
                if (self.workmateModel.companyInfo.incumbentAuthentication) {
                    NSString *titleStr = @"在职证明已认证";
                    CGSize titleS = [Tool sizeWithText:titleStr font:[UIFont fontWithName:@"PingFangSC-Regular" size:12]];
                    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(X, Y, titleS.width + 15 + 10, 36)];
                    [btn setImage:[UIImage imageNamed:@"ic_work"] forState:UIControlStateNormal];
                    [btn setImage:[UIImage imageNamed:@"ic_work"] forState:UIControlStateHighlighted];
                    [btn setTitle:titleStr forState:UIControlStateNormal];
                    [btn setTitleColor:UIColorFromRGB(0x48565A) forState:UIControlStateNormal];
                    [btn setTitleColor:UIColorFromRGB(0x48565A) forState:UIControlStateHighlighted];
                    btn.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:12];
                    btn.tag = 1000;
                    [self.tableHeaderInfoView addSubview:btn];
                    [btn setTitleEdgeInsets:UIEdgeInsetsMake(0, 5, 0, 0)];
                    
                    X += btn.mj_w + 25 * FitWidth;
                }
                if (self.workmateModel.companyInfo.realNameAuthentication) {
                    NSString *titleStr = @"实名认证";
                    CGSize titleS = [Tool sizeWithText:titleStr font:[UIFont fontWithName:@"PingFangSC-Regular" size:12]];
                    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(X, Y, titleS.width + 15 + 10, 36)];
                    [btn setImage:[UIImage imageNamed:@"ic_实名认证"] forState:UIControlStateNormal];
                    [btn setImage:[UIImage imageNamed:@"ic_实名认证"] forState:UIControlStateHighlighted];
                    [btn setTitle:titleStr forState:UIControlStateNormal];
                    [btn setTitleColor:UIColorFromRGB(0x48565A) forState:UIControlStateNormal];
                    [btn setTitleColor:UIColorFromRGB(0x48565A) forState:UIControlStateHighlighted];
                    btn.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:12];
                    btn.tag = 1001;
                    [self.tableHeaderInfoView addSubview:btn];
                    [btn setTitleEdgeInsets:UIEdgeInsetsMake(0, 5, 0, 0)];
                    
                    X += btn.mj_w + 25 * FitWidth;
                }
                if (self.workmateModel.companyInfo.laborContractAuthentication) {
                    NSString *titleStr = @"劳动合同认证";
                    CGSize titleS = [Tool sizeWithText:titleStr font:[UIFont fontWithName:@"PingFangSC-Regular" size:12]];
                    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(X, Y, titleS.width + 15 + 10, 36)];
                    [btn setImage:[UIImage imageNamed:@"合同"] forState:UIControlStateNormal];
                    [btn setImage:[UIImage imageNamed:@"合同"] forState:UIControlStateHighlighted];
                    [btn setTitle:titleStr forState:UIControlStateNormal];
                    [btn setTitleColor:UIColorFromRGB(0x48565A) forState:UIControlStateNormal];
                    [btn setTitleColor:UIColorFromRGB(0x48565A) forState:UIControlStateHighlighted];
                    btn.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:12];
                    btn.tag = 1002;
                    [self.tableHeaderInfoView addSubview:btn];
                    [btn setTitleEdgeInsets:UIEdgeInsetsMake(0, 5, 0, 0)];
                    
                    X += btn.mj_w + 25 * FitWidth;
                }
                if (self.workmateModel.companyInfo.companyEamilAuthentication) {
                    NSString *titleStr = @"公司邮箱已认证";
                    CGSize titleS = [Tool sizeWithText:titleStr font:[UIFont fontWithName:@"PingFangSC-Regular" size:12]];
                    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(X, Y, titleS.width + 15 + 10, 36)];
                    [btn setImage:[UIImage imageNamed:@"ic_mail"] forState:UIControlStateNormal];
                    [btn setImage:[UIImage imageNamed:@"ic_mail"] forState:UIControlStateHighlighted];
                    [btn setTitle:titleStr forState:UIControlStateNormal];
                    [btn setTitleColor:UIColorFromRGB(0x48565A) forState:UIControlStateNormal];
                    [btn setTitleColor:UIColorFromRGB(0x48565A) forState:UIControlStateHighlighted];
                    btn.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:12];
                    btn.tag = 1003;
                    [self.tableHeaderInfoView addSubview:btn];
                    [btn setTitleEdgeInsets:UIEdgeInsetsMake(0, 5, 0, 0)];
                    
                    X += btn.mj_w + 25;
                }
                if (self.workmateModel.companyInfo.employeeCardAuthentication) {
                    NSString *titleStr = @"员工牌已认证";
                    CGSize titleS = [Tool sizeWithText:titleStr font:[UIFont fontWithName:@"PingFangSC-Regular" size:12]];
                    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(X, Y, titleS.width + 15 + 10, 36)];
                    [btn setImage:[UIImage imageNamed:@"ic_yuangong"] forState:UIControlStateNormal];
                    [btn setImage:[UIImage imageNamed:@"ic_yuangong"] forState:UIControlStateHighlighted];
                    [btn setTitle:titleStr forState:UIControlStateNormal];
                    [btn setTitleColor:UIColorFromRGB(0x48565A) forState:UIControlStateNormal];
                    [btn setTitleColor:UIColorFromRGB(0x48565A) forState:UIControlStateHighlighted];
                    btn.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:12];
                    btn.tag = 1004;
                    [self.tableHeaderInfoView addSubview:btn];
                    [btn setTitleEdgeInsets:UIEdgeInsetsMake(0, 5, 0, 0)];
                    
                    X += btn.mj_w + 25;
                }
            }
            
        } else {
            self.headerViewHeight.constant = 93;
            self.tableHeaderInfoViewLine.hidden = YES;
        }
        
    } else {
        // 没有实名认证和企业邮箱认证
        self.headerViewHeight.constant = 93;
        
        for (UIView *con in self.tableHeaderInfoView.subviews) {
            if (con.tag == 1000 || con.tag == 1001) {
                [con removeFromSuperview];
            }
        }
        self.tableHeaderInfoViewLine.hidden = YES;
    }
    
    [self resetTableViewHeaderViewHeight];
}

- (void)resetTableViewHeaderViewHeight {
    
    CGFloat H = 426 + self.skillViewHeight.constant + 31 + self.headerViewHeight.constant + 69;
    if ([Tool isIphoneX]) {
        H += 24;
    }
    self.tableHeaderView.frame = CGRectMake(0, 0, kScreenWidth, H);
    [self.tableView reloadData];
}

/** 获取在职者问答服务列表 */
- (void)loadIncumbentQuestions {
    WS(weakSelf)
    [SendRequest getIncumbentQuestionsByUserId:self.userId result:^(NSDictionary *result, NSError *error) {
        result = [CommonUtils decryptDicWithResult:result];
        if([result[@"result"] integerValue] == 1) {
//            SaintiLog(@"获取在职者问答服务列表 = %@", [result objectForKey:@"data"]);
            NSArray *datas = [IncumbentQuestionModel mj_objectArrayWithKeyValuesArray:[result objectForKey:@"data"]];
            [weakSelf.questionArray removeAllObjects];
            [weakSelf.questionArray addObjectsFromArray:datas];
            [weakSelf resetQuestionView];
        } else {
            [CommonUtils showToast:[result objectForKey:@"msg"]];
        }
    }];
}

- (void)resetQuestionView {
    
    for (UIView *con in self.skillView.subviews) {
        if ([con isKindOfClass:[FilterButton class]]) {
            [con removeFromSuperview];
        }
    }
    
    if (self.questionArray.count > 0) {
        self.skillViewEmptyView.hidden = YES;
        self.skillViewEmptyLabel.hidden = YES;
        self.skillView.backgroundColor = [UIColor whiteColor];
        
        CGFloat btnY = 15;
        CGFloat btnX = 15;
        CGFloat btnH = 20;
        CGFloat skillViewHeight = 15 + 20 + 15;
        
        for (NSInteger i = 0; i < self.questionArray.count; i++) {
            if (i > 4) {
                break;
            }
            IncumbentQuestionModel *model = [self.questionArray objectAtIndex:i];
            
            NSString *btnTitle = [NSString stringWithFormat:@"￥%@ %@", model.price, model.skillName];
            CGFloat btnW = [Tool sizeWithText:btnTitle font:[UIFont fontWithName:@"PingFangSC-Regular" size:13]].width + 30;
            
            if (btnX + btnW + 15 > kScreenWidth - 30) {
                btnX = 15;
                btnY += 35;
                skillViewHeight += 35;
            }
            
            FilterButton *btn = [[FilterButton alloc] initWithFrame:CGRectMake(btnX, btnY, btnW, btnH)];
            btn.layer.cornerRadius = 10;
            btn.tag = i;
            [btn setTitle:btnTitle forState:UIControlStateNormal];
            [btn addTarget:self action:@selector(skillBtnClick:) forControlEvents:UIControlEventTouchUpInside];
            [self.skillView addSubview:btn];
            
            btnX += btnW + 10;
        }
        self.skillViewHeight.constant = skillViewHeight;
    } else {
        self.skillViewEmptyView.hidden = NO;
        self.skillViewEmptyLabel.hidden = NO;
        self.skillView.backgroundColor = [UIColor clearColor];
        self.skillViewHeight.constant = 85;
    }
    
    
    
    [self resetTableViewHeaderViewHeight];
}

- (void)skillBtnClick:(FilterButton *)btn {
    
}

/** 查询在职者解密服务(他人/自己查看启用状态区分) */
- (void)loadDecryptData {
    WS(weakSelf)
    [SendRequest getIncumbentDecryptByUserId:self.userId andPageNo:@"1" result:^(NSDictionary *result, NSError *error) {
        result = [CommonUtils decryptDicWithResult:result];
        if([result[@"result"] integerValue] == 1) {
//            SaintiLog(@"查询在职者解密服务 = %@", [result objectForKey:@"data"]);
            NSArray *datas = [DecryptModel mj_objectArrayWithKeyValuesArray:[[result objectForKey:@"data"] objectForKey:@"list"]];
            [weakSelf.decryptArray removeAllObjects];
            [weakSelf.decryptArray addObjectsFromArray:datas];
            [weakSelf resetDecryptView];
        } else {
            [CommonUtils showToast:[result objectForKey:@"msg"]];
        }
    }];
}

- (void)resetDecryptView {
    
    if (self.decryptArray.count > 0) {
        for (UIView *con in self.decryptionScrollView.subviews) {
            [con removeFromSuperview];
        }
        
        self.decryptionScrollView.contentSize = CGSizeMake((kScreenWidth - 128) * self.decryptArray.count, 140);
        for (NSInteger i = 0; i < self.decryptArray.count; i++) {
            WorkmateDecryptionView *wdV = [[WorkmateDecryptionView alloc] initWithFrame:CGRectMake(i * (kScreenWidth - 128), 0, kScreenWidth - 128, 140)];
            wdV.topBtn.tag = i;
            [wdV.topBtn addTarget:self action:@selector(workmateDecryptClick:) forControlEvents:UIControlEventTouchUpInside];
            [self.decryptionScrollView addSubview:wdV];
            wdV.model = [self.decryptArray objectAtIndex:i];
        }
        
        self.decryptionPageControl.numberOfPages = self.decryptArray.count;
        self.decryptionPageControl.currentPage = 0;
        
        self.decryptionView.backgroundColor = [UIColor whiteColor];
        self.decryptionScrollView.hidden = NO;
        self.decryptionViewLeftBtn.hidden = NO;
        self.decryptionViewRightBtn.hidden = NO;
        self.decryptionPageControl.hidden = NO;
        self.decryptionViewEmptyView.hidden = YES;
        self.decryptionViewEmptyLabel.hidden = YES;
    } else {
        
        self.decryptionView.backgroundColor = [UIColor clearColor];
        self.decryptionScrollView.hidden = YES;
        self.decryptionViewLeftBtn.hidden = YES;
        self.decryptionViewRightBtn.hidden = YES;
        self.decryptionPageControl.hidden = YES;
        self.decryptionViewEmptyView.hidden = NO;
        self.decryptionViewEmptyLabel.hidden = NO;
    }
    
    
}

- (void)workmateDecryptClick:(UIButton *)btn {
    DecryptModel *model = [self.decryptArray objectAtIndex:btn.tag];
    EntryProtectDetailVC *detail = [[EntryProtectDetailVC alloc] init];
    detail.decryptId = model.incumbentDecryptId;
    detail.index = 1;
    [self.navigationController pushViewController:detail animated:YES];
}

/** 查询在职者保过服务 */
- (void)loadAssurePassData {
    WS(weakSelf)
    [SendRequest getIncumbentAssurePassByUserId:self.userId andPageNo:@"1" result:^(NSDictionary *result, NSError *error) {
        result = [CommonUtils decryptDicWithResult:result];
        if([result[@"result"] integerValue] == 1) {
//            SaintiLog(@"查询在职者保过服务 = %@", [result objectForKey:@"data"]);
            NSArray *datas = [AssurePassModel mj_objectArrayWithKeyValuesArray:[[result objectForKey:@"data"] objectForKey:@"list"]];
            [weakSelf.assurePassArray removeAllObjects];
            [weakSelf.assurePassArray addObjectsFromArray:datas];
            [weakSelf resetAssurePassView];
        } else {
            [CommonUtils showToast:[result objectForKey:@"msg"]];
        }
    }];
}

- (void)resetAssurePassView {
    if (self.assurePassArray.count > 0) {
        for (UIView *con in self.entryInsuranceScrollView.subviews) {
            [con removeFromSuperview];
        }
        
        NSInteger count = self.assurePassArray.count;
        self.entryInsuranceScrollView.contentSize = CGSizeMake((kScreenWidth - 128) * count, 140);
        for (NSInteger i = 0; i < count; i++) {
            WorkmateEntryInsurance *eiV = [[WorkmateEntryInsurance alloc] initWithFrame:CGRectMake(i * (kScreenWidth - 128), 0, kScreenWidth - 128, 140)];
            eiV.topBtn.tag = i;
            [eiV.topBtn addTarget:self action:@selector(workmateAssurePassClick:) forControlEvents:UIControlEventTouchUpInside];
            [self.entryInsuranceScrollView addSubview:eiV];
            eiV.model = [self.assurePassArray objectAtIndex:i];
        }
        
        self.entryInsurancePageControl.numberOfPages = count;
        self.entryInsurancePageControl.currentPage = 0;
        
        self.insuranceView.backgroundColor = [UIColor whiteColor];
        self.entryInsuranceScrollView.hidden = NO;
        self.insuranceViewLeftBtn.hidden = NO;
        self.insuranceViewRightBtn.hidden = NO;
        self.entryInsurancePageControl.hidden = NO;
        self.insuranceViewEmptyView.hidden = YES;
        self.insuranceViewEmptyLabel.hidden = YES;
    } else {
        
        self.insuranceView.backgroundColor = [UIColor clearColor];
        self.entryInsuranceScrollView.hidden = YES;
        self.insuranceViewLeftBtn.hidden = YES;
        self.insuranceViewRightBtn.hidden = YES;
        self.entryInsurancePageControl.hidden = YES;
        self.insuranceViewEmptyView.hidden = NO;
        self.insuranceViewEmptyLabel.hidden = NO;
    }
    
}

- (void)workmateAssurePassClick:(UIButton *)btn {
    AssurePassModel *model = [self.assurePassArray objectAtIndex:btn.tag];
    EntryProtectDetailVC *detail = [[EntryProtectDetailVC alloc] init];
    detail.assurePassId = model.incumbentAssurePassId;
    detail.companyId = model.publisherCompanyId;
    detail.index = 2;
    [self.navigationController pushViewController:detail animated:YES];
}

/** 查询在职者回答的问答信息 */
- (void)loadAnswersDataList {
    WS(weakSelf)
    [SendRequest getQuestiosOfIncumbentRespondentByUserId:self.userId andPageNo:[NSString stringWithFormat:@"%zd", _pageNo] result:^(NSDictionary *result, NSError *error) {
        result = [CommonUtils decryptDicWithResult:result];
        if([result[@"result"] integerValue] == 1) {
            SaintiLog(@"查询在职者回答的问答信息列表 = %@", [result objectForKey:@"data"]);
            
            NSMutableArray *datas = [NSMutableArray array];
            NSArray *tempArr = [[result objectForKey:@"data"] objectForKey:@"list"];
            for (NSInteger i = 0; i< tempArr.count; i++) {
                QuestionModel *models = [QuestionModel mj_objectWithKeyValues:tempArr[i]];
                NSArray *anwsers = tempArr[i][@"anwsers"];
                if (anwsers.count > 0) {
                    QuestionAnwserModel *qaModel = [QuestionAnwserModel mj_objectWithKeyValues:anwsers[0]];
                    models.anwserModel = qaModel;
                }
                [datas addObject: models];
            }
            if (_pageNo == 1) {
                [weakSelf.reviewArray removeAllObjects];
                [weakSelf.reviewArray addObjectsFromArray:datas];
                [weakSelf.tableView reloadData];
                [weakSelf.tableView.mj_header endRefreshing];
                [weakSelf.tableView.mj_footer endRefreshing];
            } else {
                [weakSelf.reviewArray addObjectsFromArray:datas];
                [weakSelf.tableView reloadData];
                if (datas.count < kNumOfPageIntSize) {
                    [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];
                } else {
                    [weakSelf.tableView.mj_footer endRefreshing];
                }
            }
        } else {
            [CommonUtils showToast:[result objectForKey:@"msg"]];
            if (_pageNo == 1) {
                [weakSelf.tableView.mj_header endRefreshing];
            } else {
                [weakSelf.tableView.mj_footer endRefreshing];
            }
        }
    }];
}

#pragma mark - UIScrollViewDelegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView == self.entryInsuranceScrollView) {
        CGFloat scrollX = scrollView.contentOffset.x;
        NSInteger index = scrollX / (kScreenWidth - 128) + 0.5;
        self.entryInsurancePageControl.currentPage = index;
    } else if (scrollView == self.decryptionScrollView) {
        CGFloat scrollX = scrollView.contentOffset.x;
        NSInteger index = scrollX / (kScreenWidth - 128) + 0.5;
        self.decryptionPageControl.currentPage = index;
    } else {
        
    }
}

- (IBAction)backBtnClick:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - UITableViewDelegate, UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.reviewArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identifier = @"WorkmateAnswersTableViewCell";
    AnswersTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    cell.selectionStyle =  UITableViewCellSelectionStyleNone;
    QuestionModel *model = self.reviewArray[indexPath.row];
    cell.model = model;
    cell.audioButton.tag = indexPath.row;
    cell.wordButton.tag = indexPath.row;
    [cell.audioButton addTarget:self action:@selector(voiceButonClick:) forControlEvents:UIControlEventTouchUpInside];
    [cell.wordButton addTarget:self action:@selector(voiceButonClick:) forControlEvents:UIControlEventTouchUpInside];
    return cell;
}
-(void)voiceButonClick:(UIButton *)sender{
    QuestionModel *questionModel = self.reviewArray[sender.tag];
    UserModel *user = [NSUDManager currentUser];
    QuestionAnwserModel *model =  questionModel.anwserModel;
    if (model) {
        if (model.isPurchased || [user.userId isEqualToString:[NSString stringWithFormat:@"%zd",model.publisherId]]||[user.userId isEqualToString:questionModel.publisherId]) {
            if ([model.anwserType isEqualToString:@"ANWSER_TYPE_SOUND"]) {
                /**语音*/
                [PlayVoice audioPlayWithUrl:model.soundKey];
            }else  if ([model.anwserType isEqualToString:@"ANWSER_TYPE_WORDS"]) {
                /**文字*/
                
            }
            
        }else{
            /**偷听*/
            [self showProgressWithView:self.view animated:YES];
            __weak typeof(self) weakSelf = self;
            [ [CommonUtils sharedCommonUtils] payWithType:@"GOODS_TYPE_TT" relatedId:[NSString stringWithFormat:@"%zd",model.anwserId] result:^(NSDictionary *result, BOOL paySuccess) {
                [self hideProgress:self.view animated:YES];
                if (paySuccess) {
                    
                    [weakSelf.tableView.mj_header beginRefreshing];
                    [PlayVoice audioPlayWithUrl:model.soundKey];
                }else{
//                    [CommonUtils showToast:[result objectForKey:@"msg"]];
                }
            }];
        }
    }
    
}
- (void )tableView:(UITableView *)tableView didSelectRowAtIndexPath:(nonnull NSIndexPath *)indexPath
{
    QuestionModel *questionModel = self.reviewArray[indexPath.row];
    HelpDetailVC *vc = [[HelpDetailVC alloc]init];
    vc.relatedId = [NSString stringWithFormat:@"%@",questionModel.questionId];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
    
}

#pragma mark - 向TA提问按钮点击事件

- (IBAction)questionBtnClick:(id)sender {
    if ([self.workmateModel.userId isEqualToString:[NSUDManager currentUser].userId] || [self.workmateModel.userId integerValue] == [[NSUDManager currentUser].userId integerValue]) {
        [CommonUtils showToast:@"不能向自己提问"];
    } else {
        DirectionalQuestionViewController *vc = [[DirectionalQuestionViewController alloc] init];
        vc.workmateModel = self.workmateModel;
        vc.companyId = self.workmateModel.companyId;
        [self.navigationController pushViewController:vc animated:YES];
    }
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
    
    self.navigationController.navigationBarHidden = YES;
}
-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
}
-(void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
    self.navigationController.navigationBarHidden = NO;
}

- (NSMutableArray *)questionArray {
    if (!_questionArray) {
        _questionArray = [[NSMutableArray alloc] init];
    }
    return _questionArray;
}

- (NSMutableArray *)decryptArray {
    if (!_decryptArray) {
        _decryptArray = [[NSMutableArray alloc] init];
    }
    return _decryptArray;
}

- (NSMutableArray *)assurePassArray {
    if (!_assurePassArray) {
        _assurePassArray = [[NSMutableArray alloc] init];
    }
    return _assurePassArray;
}

- (NSMutableArray *)reviewArray {
    if (!_reviewArray) {
        _reviewArray = [[NSMutableArray alloc] init];
    }
    return _reviewArray;
}


#pragma mark - 获取用户对同事的收藏信息，并判断同事是否收藏

- (void)getUserCollected {
    WS(weakSelf)
    [SendRequest getUserCollectedWithType:@"COLLECTION_TYPE_USER" result:^(NSDictionary *result, NSError *error) {
        [weakSelf hideProgress:self.view animated:YES];
        result = [CommonUtils decryptDicWithResult:result];
        if([result[@"result"] integerValue] == 1) {
            SaintiLog(@"用户对同事的收藏信息列表 = %@", [[result objectForKey:@"data"] objectForKey:@"list"]);
            
            NSArray *datas = [[result objectForKey:@"data"] objectForKey:@"list"];
            if (datas && datas.count > 0) {
                for (NSDictionary *dict in datas) {
                    NSString *userId_dict = [[dict objectForKey:@"details"] objectForKey:@"userId"];
                    if ([userId_dict integerValue] == [self.userId integerValue]) {
                        self.collectBtn.selected = YES;
                        break;
                    }
                }
            }
        } else {
            [CommonUtils showToast:[result objectForKey:@"msg"]];
        }
    }];
}

@end
